<?php

/**
 * @Project NUKEVIET 3.x - GETNEWS
 * @Author ĐẶNG ĐÌNH TỨ (dlinhvan@gmail.com)
 * @Copyright (C) 2010 webdep24.com All rights reserved
 * @Createdate 10/08/2012 08:00
 */

if( ! defined( 'NV_IS_FILE_ADMIN' ) ) die( 'Stop!!!' );
$time_start = microtime( true );
global $urlimages, $urlink, $module, $folder, $global_config, $module_config;

$contents = "";
$folder = date( "Y_m" );
$do = 0;
$catids = "";
$error = "";
$data = array();
$hauto = "bdv";
$module = $module_config[$module_name]['module'];

$urlimages = '';
$imglink = '';
$urlink = "";

$url = filter_text_input( 'cat', 'post', '' );
$url = 'http://baodatviet.vn/Home/chinhtrixahoi.rss';

$do = $nv_Request->get_int( 'do', 'post', 0 );
$catids = $nv_Request->get_int( 'catids', 'post', 0 );
$sourceid = $nv_Request->get_int( 'sourceid', 'post', 0 );
nguontin( $sourceid, $url );
$contents .= "<table class=\"tab1\">
      <thead>
         <tr>
            <td>
               " . $lang_module['info_bdv'] . "
            </td>
         </tr>
      </thead></table>";

$contents .= nv_get_cat( $module, $hauto, $url, $sourceid, $catids );

$contents .= xuat_html( 0 );

// lay thong tin html
$loop = "div.categories_item_small";
$title_t = "div.categories_item_small_title a";
$link_t = "div.categories_item_small_title a";
$home_t = "div.categories_item_small_summary span";
$img_t = "div.categories_item_small_title a img";

if( $do )
{

	$id_block_content = array_unique( $nv_Request->get_typed_array( 'bids', 'post', 'int', array() ) );

	$getContent = new UrlGetContents( $global_config );
	$html = $getContent->get( $url );
	if( strpos( $url, '.rss' ) )
	{
		$xml_source = $getContent->get( $url );
		$allowed_html_tags = array_map( "trim", explode( ",", NV_ALLOWED_HTML_TAGS ) );
		$allowed_html_tags = "<" . implode( "><", $allowed_html_tags ) . ">";
		if( $xml = simplexml_load_string( $xml_source ) )
		{
			if( isset( $xml->channel ) )
			{
				foreach( $xml->channel->item as $item )
				{
					$title = strip_tags( $item->title );
					$alias = change_alias( $title );
					$data[$alias]['title'] = $title;
					$data[$alias]['alias'] = $alias;
					$des = $item->description;

					$data[$alias]['hometext'] = trim( strip_tags( preg_replace( "'<a[^>]*>.*</a>'siU", "", $des ) ) );

					$data[$alias]['link'] = trim( strip_tags( $item->link ) );
					$data[$alias]['time'] = nv_date( "l - d/m/Y  H:i", strtotime( $item->pubDate ) );
					$img = str_replace( array( ' ', 'http://media.baodatviet.vnhttp//cms.baodatviet.vn' ), array( '%20', 'http://media.baodatviet.vn' ), ( string )$item->enclosure["url"] );
					$data[$alias]['homefile'] = $img;
				}
			}
		}
		//$hometext->clear();
		//unset( $hometext );
	}
	else
	{
		$error .= "Dữ liệu nguồn bị thay đổi hoặc link chuyên mục không đúng";
		break;
	}
	//var_dump($data);
	$contents .= xuat_html( 1 ); // xuat thanh  noi dung tieu de
	$ii = 1;
	foreach( $data as $alias => $array )
	{
		$sql = "SELECT COUNT(*) FROM `" . NV_PREFIXLANG . "_" . $module . "_rows` WHERE `alias`=" . $db->dbescape_string( $alias );
		$result = $db->sql_query( $sql );
		list( $exist ) = $db->sql_fetchrow( $result );
		if( empty( $exist ) )
		{
			$html = $getContent->get( $array['link'] );
			$html1 = str_get_html( $html );
			if( ! ( $html1->find( 'span#DetailtBody1_lbBody', 0 ) ) )
			{
				$error .= "Dữ liệu nguồn bị thay đổi";
				break;
			}
			else
			{
				$publtime = NV_CURRENTTIME - rand( 100, 600 );

				if( empty( $error ) )
				{
					$html1->find( 'a#SubBody1_rptOtherList_ctl10_lnkOther', 0 )->outertext = "";
					$html1->find( 'a#ctl00___rptOtherList_ctl01_lnkOther', 0 )->outertext = "";
					$body = $html1->find( 'span#DetailtBody1_lbBody', 0 )->innertext;

					$body = preg_replace( "'<script[^>]*>.*</script>'siU", '', $body );
					$body = preg_replace( '#<td style="padding-left:0px;(.*?)>(.*?)</td>#is', '', $body );
					$bodytext1 = img_link( $body, $urlink );
					$bodytext = load_image( $bodytext1, $urlink );
					$bodytext = stripwhitespace( $bodytext );

					if( ! empty( $bodytext1 ) )
					{
						$imgfile = nv_get_images( $bodytext1 );
						if( empty( $imgfile ) ) $imgfile = "";
						if( ! empty( $imgfile ) and $module_config[$module_name]['load_image'] == 1 )
						{
							$img_file = nv_filter( $imgfile );
							$homefile = $img_file[0];
							$homeimgthumb = $img_file[1];
						}
						elseif( ! empty( $array['homefile'] ) and ! empty( $imgfile ) )
						{
							$homefile = $imgfile;
							$homeimgthumb = $array['homefile'];
						}
						elseif( ! empty( $array['homefile'] ) and empty( $imgfile ) )
						{
							$homefile = $array['homefile'];
							$homeimgthumb = "";
						}
						elseif( ! empty( $imgfile ) )
						{
							$homefile = $imgfile;
							$homeimgthumb = "";
						}
						else
						{
							$homefile = "";
							$homeimgthumb = "";
						}
					}
					else
					{
						$error .= "Lỗi Không Cập Nhật Được Nội Dung";
						break;
					}
					if( empty( $error ) )
					{
						nv_save_data( $module, $catids, $sourceid, $array['title'], $array['alias'], $array['hometext'], $publtime, $homefile, $homeimgthumb, nv_convert( $bodytext ), $array['link'], $id_block_content );
						$contents .= "<table width=\"100%\" border=\"1\">";
						$contents .= "<tr><td width='5%' style=\"height:26px;\" align=center><strong>$ii</strong></td><td width='61%'><strong><a style=\"padding-left:10px;text-decoration:none;color:black\" href=\"" . $array['link'] . "\" target='_blank'>" . $array['title'] . "<img src=\"" . NV_BASE_SITEURL . "themes/default/images/" . $module_name . "/newnew.gif\"></a></strong></td><td width='34%' align=center style=\"width:100px;\"><strong>" . date( 'H:i, d-m-Y', $publtime ) . "</strong></td></tr>";
						$contents .= "</table>";
					}
					$html1->clear();
					unset( $html1, $html );
				}
			}
		}
		++$ii;
		if( $ii > 1 ) break;
	}
	if( ! empty( $error ) )
	{
		nv_sendmail( array( $global_config['site_name'], $global_config['site_email'] ), $global_config['error_send_email'], "Error get new :" . $error, "Error get new :" . $error . " từ website " . NV_SERVER_NAME . " nguồn " . $url . " " . $global_config['site_email'] );
	}
	else
	{
		optimize();
		nv_del_moduleCache( $module );
	}

}
$time_end = microtime( true );
$time = $time_end - $time_start;
$contents .= "<br>" . $error . "\n";
$contents .= "<br>Thời gian cập nhật: $time giây\n";

//include ( NV_ROOTDIR . "/includes/header.php" );
echo nv_admin_theme( $contents );
//include ( NV_ROOTDIR . "/includes/footer.php" );


?>